cmake_minimum_required(VERSION 3.0)

project(communication)

message("CMAKE_HOST_SYSTEM_NAME: ${CMAKE_HOST_SYSTEM_NAME}")
# 平台检测，或者指定编译工具链等
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
    set(IS_LINUX 1)
else()
    #默认为windows的mingw64平台
    set(IS_WINDOWS 1)
endif()

file(GLOB_RECURSE SRC_LIST ./*.cpp ./*.cc ./*.cxx ./*.c ./*.h ./*.hpp)

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/../common)
include_directories(${PROJECT_SOURCE_DIR}/../../thirdparty/inc)
#link_directories(${PROJECT_SOURCE_DIR}/../../thirdparty/lib)

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../../bin)
add_library(${PROJECT_NAME} SHARED ${SRC_LIST})

# 设置链接选项避免符号冲突
if(IS_WINDOWS)
    # Windows平台 - 使用静态链接避免DLL冲突
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    
    # 链接系统库
    target_link_libraries(${PROJECT_NAME} -lwsock32 -lws2_32)
    
    # 只链接需要的第三方库，避免链接包含标准库的DLL
    # 注意：不直接链接包含libstdc++的DLL文件
    target_link_libraries(${PROJECT_NAME} 
        ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/win/mosquitto/mosquitto.dll
        ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/win/civetweb/libcivetweb.dll
        ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/win/civetweb/libcivetweb-cpp.dll
        pthread m)
else()
    # Linux平台
    file(GLOB_RECURSE MOSQUITTO_LIB ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/linux/mosquitto/*.so)
    file(GLOB_RECURSE CIVETWEB_LIB ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/linux/civetweb/*.so)
    target_link_libraries(${PROJECT_NAME} ${MOSQUITTO_LIB} ${CIVETWEB_LIB} pthread dl rt m)
endif()
